
package com.yuan.graduate;

import com.yuan.application.BaseActivity;
import com.yuan.control.SettingControl;
import com.yuan.prefence.PreferenceKeys;
import com.yuan.unit.PreferenceUtil;
import com.yuan.view.UserAgreementView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;

/**
 * 引导页 第一次安装时会显示欢迎页 以后再自动登录时会有作用
 * 
 * @author YUAN
 */
@SuppressLint("HandlerLeak")
public class SplashActivity extends BaseActivity {
    private boolean isFirstUsing = false;
    private UserAgreementView useragreementview = null;
    private Context mContext = null;
    private Handler mHandler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what)
                {
                    case MSG_ISFIRSTUSE:
                        startMainActivity();
                        break;
                }
            }
        };
        // startService(new Intent(SERVER_NAME.LOCAL_SERVER));
        mContext = this;
        CheckToGo();
    }

    /**
     * @author YuanWei 检查是否是第一次启动，如果为第一次启动则启动UserAgreementView界面 如果不是则直接跳到登陆界面、
     */
    private void CheckToGo() {
        // TODO Auto-generated method stub
        isFirstUsing = PreferenceUtil.getInstance().getBoolean(ISFIRSTUSE, true);
        if (isFirstUsing)
        {
            PreferenceUtil.getInstance().putBoolean(ISFIRSTUSE, false);
            useragreementview = new UserAgreementView(mContext);
            addContentView(useragreementview, new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT));
            SettingControl.RetConfig();
        }
        else
        {
            startMainActivity();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void showWelcomePage() {
        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    mHandler.sendEmptyMessage(MSG_ISFIRSTUSE);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void startMainActivity() {
        // TODO Auto-generated method stub
        String patternString = PreferenceUtil.getInstance().getString(PreferenceKeys.LOCK_KEY,
                null);
        if (patternString != null) {
            Intent mIntent = new Intent(SplashActivity.this, LockActivity.class);
            mIntent.putExtra("isAutoLogin", true);
            startActivity(mIntent);
            finish();
            return;
        }
        Intent mIntent = new Intent(SplashActivity.this, LoginActivity.class);
        if (SettingControl.isAutoLogin())
        {
            mIntent.putExtra("isAutoLogin", true);
        }
        else {
            mIntent.putExtra("isAutoLogin", false);
        }
        startActivity(mIntent);
        finish();
    }

    @Override
    public void onDestroy() {
        useragreementview = null;
        mContext = null;
        super.onDestroy();
    }

    public void onMyDestroy() {
        useragreementview = null;
        mContext = null;
        super.onMyDestroy();

    }
}
